import router from "@/router"
// @ts-ignore
import NProgress from "nprogress"
import "nprogress/nprogress.css"
import appStore from "@/pinia"
import { storeToRefs } from "pinia"

NProgress.configure({
    easing: "ease", // 动画方式
    showSpinner: true, // 是否显示加载ico
    trickleSpeed: 200, // 自动递增间隔
    minimum: 0.4, // 更改启动时使用的最小百分比
})

// const whiteList = ['/login', '/redirect']

/**
 * beforeEach有三个参数:
 * to: 即将要进入的目标
 * from: 当前导航正要离开的路由
 * 可选的第三个参数 next:进入下一个目标
 */
router.beforeEach(async (to, form?, next) => {
    NProgress.start()
    // 获取登录用户名及token
    const { token } = storeToRefs(appStore.userModule);
    // 用户登录判断
    if(to.path == '/login'){
        next()
    }else {
        if (token.value !== '') {
            next()
        }else {
            next({ path: '/login', query: { redirect: to.path } })
        }
    }
})

// 全局后置守卫
router.afterEach((to?: any, form?: any) => {
    NProgress.done()
})